package com.dxtx.test;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
//    TagCloudView cloudView;
//    private int[] ss = {R.mipmap.baby1, R.mipmap.jt, R.mipmap.liy, R.mipmap.lyf1, R.mipmap.ym, R.mipmap.mm1, R.mipmap.mm2, R.mipmap.baby1, R.mipmap.jt, R.mipmap.liy, R.mipmap.lyf1, R.mipmap.ym, R.mipmap.mm1, R.mipmap.mm2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= 21) {
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        TextView textView = (TextView) findViewById(R.id.wode);

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.mipmap.jt, opts);
        int max = Math.max(opts.outWidth, opts.outHeight);
        if (max > 80) {
            opts.inSampleSize = max / 80;
        }
        opts.inJustDecodeBounds = false;
        Bitmap br = BitmapFactory.decodeResource(getResources(), R.mipmap.jt, opts);

        SpannableString string = new SpannableString("Left");
        ImageSpan imageSpan1 = new ImageSpan(br, DynamicDrawableSpan.ALIGN_BOTTOM);
        string.setSpan(imageSpan1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString string1 = new SpannableString("aaaa");
        ImageSpan imageSpan = new ImageSpan(br, DynamicDrawableSpan.ALIGN_BOTTOM);
        string1.setSpan(imageSpan, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        String source = "我是一条画删除线的文字";
        SpannableString string2 = new SpannableString(source);
        string2.setSpan(new StrikethroughSpan(), 0, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        string2.setSpan(new ForegroundColorSpan(0xffaaaaaa), 0, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        string2.setSpan(new BackgroundColorSpan(0x30ff00ff), 0, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        string2.setSpan(new RelativeSizeSpan(.8f), 0, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        String s = "我是一条可点击的文字";
        SpannableString string3 = new SpannableString(s);
        //设置下划线文字
        string3.setSpan(new UnderlineSpan(), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        string3.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "你点我干嘛", Toast.LENGTH_SHORT).show();
            }
        }, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        String s1 = "";
        if (s1.startsWith("data")) {
            int index = s1.indexOf("base64,");
            if (index > -1) {
                s1 = s1.substring(index + 7);
            } else {
                //错误的数据格式
            }
        }

        SpannableStringBuilder sb = new SpannableStringBuilder();
        sb.append(string);
        sb.append("我的文字");
        sb.append(string1);
        sb.append(string2);
        sb.append(string3);
        sb.append(s1);


        textView.setText(sb);
        //设置超链接可点击
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setLinkTextColor(0xff0000ff);

        ListView listview = (ListView) findViewById(R.id.listview);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("" + i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_ring_menu, R.id.textView1, list);
        listview.setAdapter(adapter);

        final FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setId(R.id.second);
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
        FrameLayout f = new FrameLayout(this);
        f.setLayoutParams(new ViewGroup.LayoutParams(-1, 400));
        f.setBackgroundColor(Color.GRAY);
        listview.addHeaderView(f);
        listview.addHeaderView(frameLayout);
        final FrameLayout title = (FrameLayout) findViewById(R.id.frame);
        title.setId(R.id.third);
        getSupportFragmentManager().beginTransaction().add(R.id.second, new TitleFragment()).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.third, new TitleFragment()).commit();

        listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            boolean inListView = true;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((frameLayout.getTop() <= 0 || firstVisibleItem > 0) && inListView) {
                    title.setVisibility(View.VISIBLE);
                    inListView = false;
                } else if (frameLayout.getTop() > 0 && !inListView) {
                    title.setVisibility(View.INVISIBLE);
                    inListView = true;
                }
            }
        });
    }


}
